package classifier;

import java.io.FileNotFoundException;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.io.*;
/**
 * This class imports a file and creates a decisiontree based on the values of a text file.
 * Change the value in Main for the location of the file.
 * The text file is named "TrainingSet.txt" and can be found on blackboard
 */
public class DTFile {
	private String filepath;
	private FeatureType ft;
	private int countFeatures;
	private String[] featureNames;
    private HashMap<Item, String> trainingSet = new HashMap<Item, String>();
    private HashMap<String, FeatureType> featureMap = new HashMap<String, FeatureType>();	
	/**
	 * @param filepath = the path where the file TrainingSet.txt is located
	 * @param featureNames contains an array of strings with the name of the features. Example: CruiseControl
	 */
	public DTFile(String filepath, String[] featureNames){
		this.filepath = filepath;
		this.featureNames = featureNames;
	}
	/**
	 * This function creates the DecisionTree based on the values in the file.
	 * @return DecisionTree
	 */
	public DecisionTree buildTree(){
			File file = new File(filepath);
			Scanner lines = null;
			try {
				lines = new Scanner(file);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

			String line = lines.nextLine();
			
			String[] splitline = line.split(";");
			//gets the number of features from the file.
			int featureCount = Integer.parseInt(splitline[1]);
			line = lines.nextLine();
			splitline = line.split(";");
			//gets the number of items from the file.
			int itemCount = Integer.parseInt(splitline[1]);
			
			//featureType.. Change this for more possible values.
			FeatureType ft = new FeatureType("yn", new String[]{"0", "1"});
			
			while(lines.hasNext()){
				line = lines.nextLine();
				splitline = line.split(";");
				//name of the item
				String itemName = splitline[0];
				//this will contain the feature values (Example: Feature has value 0 or 1)
				String[] featureValues = new String[featureNames.length];
				//This will contain all the different features with their values and featuretype
				Feature[] features = new Feature[featureNames.length];
				for(int i = 0; i < features.length; i++){
					//first line in File is the item name. So we want all the numbers > 0 and 
					//smaller than features.length (last value)
					featureValues[i] =  splitline[i + 1];

				}
				
				for(int i = 0; i < features.length; i++){
					//featureNames from constructor contains all the names of the features.
					features[i] = new Feature(featureNames[i], featureValues[i], ft);

				}
				//creates trainingSet with the item and their features, and the category
				//(which is the last value of the line)
				trainingSet.put(new Item(itemName, features), splitline[splitline.length - 1]);

				
				
			}

			for(int i = 0; i<featureNames.length;i++){
				//map of feature names + possible value (feature type)
                        featureMap.put(featureNames[i],ft);
 
			
			}
			DecisionTree tree = new DecisionTree(trainingSet, featureMap);
			return tree;

	}
}
